﻿using System;
using CcrFlows.Core;
using Microsoft.Ccr.Core;

namespace SampleFlow
{
    class Program_ParallelFlow
    {
        static void Main()
        {
            var flow = Flow<string>
                .Fork<string>(ForkedSplitIntoWords)
                    .Do<string, EmptyValue>(TransformWords, LogWords);

            //var flow = Flow<string>
            //    .Do<string>(SplitIntoWords)
            //    .Fork()
            //        .Do<string, EmptyValue>(TransformWords, LogWords);

            flow.Input.Post("the quick brown fox jumps over the lazy dog");

            Arbiter.Activate(
                new DispatcherQueue(),
                Arbiter.Receive(
                    true,
                    flow.Output0,
                    Console.WriteLine
                    )
                );

            Console.ReadLine();
        }
        

        static void SplitIntoWords(string text, Port<string> words)
        {
            foreach (string word in text.Split(' '))
                words.Post(word);
        }

        static void ForkedSplitIntoWords(string text, Port<string> words0, Port<string> words1)
        {
            foreach (string word in text.Split(' '))
            {
                words0.Post(word);
                words1.Post(word);
            }
        }
        
        static void TransformWords(string word, Port<string> output)
        {
            output.Post(word.ToUpper());
        }

        static void LogWords(string word, Port<EmptyValue> output)
        {
            Console.WriteLine("  logging: {0}", word);
        }
    }
}
